package com.yyk.yykywd.mvvm.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.yyk.yykywd.databinding.ItemMeetingInvitationBinding
import com.yyk.yykywd.mvvm.entity.MeetingBean

/**
 * 文件描述：
 * Created by guo on 2019-11-19.
 */
class MeetingAdapter1
    : ListAdapter<MeetingBean.Data, MeetingAdapter1.ViewHolder>(MeetingDiffCallback()) {
    private var mListener: ((Int, View) -> Unit)? = null

    //lambda表达式作为函数中的参数
    fun setOnItemChildClickListener(mListener: (position: Int, view: View) -> Unit) {
        this.mListener = mListener
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = getItem(position)
        holder.itemView.setOnClickListener {
            mListener?.invoke(position,it)
        }
        holder.binding.meeting = data
        holder.binding.executePendingBindings()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemMeetingInvitationBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    class ViewHolder(val binding: ItemMeetingInvitationBinding)
        : RecyclerView.ViewHolder(binding.root)
}

class MeetingDiffCallback : DiffUtil.ItemCallback<MeetingBean.Data>() {

    override fun areItemsTheSame(oldItem: MeetingBean.Data, newItem: MeetingBean.Data): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: MeetingBean.Data, newItem: MeetingBean.Data): Boolean {
        return oldItem == newItem
    }
}